package com.pe.cineplanet.proyectocine.repository.hibernate;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.pe.cineplanet.proyectocine.domain.Local;
import com.pe.cineplanet.proyectocine.domain.Sala;
import com.pe.cineplanet.proyectocine.repository.SalaRepository;

 
@Repository
public class SalaRepositoryHibernate extends HibernateDaoSupport 
implements SalaRepository{
	
	//constructor
	@Autowired
	public SalaRepositoryHibernate(SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}

	public List<Sala> obtenerSalas() {
		return getHibernateTemplate().find("from Sala");  
	}

	public Sala obtenerSala(int id_Sala) {
		return (Sala) getSession().createQuery("from Sala u where u.id_Sala=?").setInteger(0, id_Sala).uniqueResult();
	}

	public void grabarSala(Sala obj) {
		getHibernateTemplate().saveOrUpdate(obj);
		
	}

	public void removerSala(int id_Sala) {
		Sala obj = obtenerSala(id_Sala);
		getHibernateTemplate().delete(obj);
		
	}

	public boolean deleteSala(int id_Sala) {
		if(id_Sala != 0){
			Sala obj = obtenerSala(id_Sala);
			getHibernateTemplate().delete(obj);
			return true;
		}
		else
			return false;
	}

	public List<Sala> obtenerSalasXEstatus(boolean status) {
		String sql =  "from Sala u where u.sts_Sala=?";
		return getHibernateTemplate().find(sql, new Object[]{status});
	}

	public List<Sala> obtenerSalasXLocal(Local id_local) {
		String sql =  "from Sala u where u.local=?";
		return getHibernateTemplate().find(sql, new Object[]{id_local});
	}

}
